//********************************************************************************************************************//
//ORGANIZATION:
//AUTHOR: bpeng(bogers.peng@gmail.com)
//SUMMARY:
//********************************************************************************************************************//
#include "../Procedure.h"
#include "../Argument.h"
#include "System/Base/Exceptions.h"

using namespace Native::Compiling;
using namespace Native::Compiling::Sql;

CProcedure::CProcedure()
{
}

CProcedure::CProcedure(C_PROCEDURE& that) :
    CField(that),
    _owner(that._owner),
    _name(that._name),
    _returned_variable(that._returned_variable),
    _arguments(that._arguments)
{
}

CProcedure::CProcedure(C_PROCEDURE&& that) :
    CField(that),
    _owner(that._owner),
    _name(that._name),
    _returned_variable(that._returned_variable),
    _arguments(that._arguments)
{
}

CProcedure::~CProcedure()
{
}

wstring CProcedure::ToString() const
{
    return EMPTY_STRING;
}

bool CProcedure::operator==(C_PROCEDURE& rvalue) const
{
    return false;
}

bool CProcedure::operator!=(C_PROCEDURE& rvalue) const
{
    return !operator==(rvalue);
}

void CProcedure::Clear()
{
    CField::Clear();

    _owner = EMPTY_STRING;
    _name = EMPTY_STRING;
    _returned_variable = EMPTY_STRING;
    _arguments.Clear();
}

CString CProcedure::ToCode() const
{
    return EMPTY_STRING;
}

void CProcedure::Append(C_ARGUMENT& arguement) throw()
{
    try {
        _arguments.Add(arguement.Name(), arguement);
    } catch(CException& e) {
        NATIVE_THROW_CAUGHT(L"Append", e);
    }
};

CArray<CArgument> CProcedure::Query(C_STRING& name) const
{
    CArray<CArgument> queried;
    auto cursor = _arguments.Find(name);

    if (cursor.Exist()) {
        queried.Append(_arguments.Value(cursor.Key()));
    }

    return move(queried);
};

C_PROCEDURE& CProcedure::operator=(C_PROCEDURE& rvalue)
{
    CField::operator=(rvalue);

    _owner = rvalue._owner;
    _name = rvalue._name;
    _returned_variable = rvalue._returned_variable;
    _arguments = rvalue._arguments;

    return *this;
}